package com.kevin.springboot.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @Classname MyLocaleResolver
 * @Description 自定义国际话解析器
 * @Date 2020/6/24 23:55
 * @Created by kevin
 */
public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //huoqu请求中的语言参数
        String language = request.getParameter("language");
        //获取默认的国际话语言
        Locale locale = Locale.getDefault();
        //判断请求中是否鞋带语言参数
        System.out.println("debug===>"+language);
        if (!StringUtils.isEmpty(language)) {
            //zh_CN
            String[] split = language.split("_");
            //国家地区
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
